package com.ksyun.ks3.demo;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ksyun.ks3.dto.Adp;
import com.ksyun.ks3.dto.CannedAccessControlList;
import com.ksyun.ks3.dto.PutAdpResult;
import com.ksyun.ks3.dto.PutObjectResult;
import com.ksyun.ks3.service.Ks3Client;
import com.ksyun.ks3.service.request.PutAdpRequest;
import com.ksyun.ks3.service.request.PutObjectRequest;

public class Demo {
	public static void main(String [] args){
		/**
		 * 即存储空间名称
		 */
		String bucket = "yyy";
		/**
		 * 在ks3上保存的文件名,全路径
		 */
		String key = "souce.3gp";
		/**
		 * 要上传的文件
		 */
		File file = new File("/Users/lijunwei/Movies/野生动物.3gp");
		/**
		 * 使用 http://ks3.ksyun.com/console.html#/setting 的 AK/SK初始化一个客户端
		 */
		Ks3Client client = new Ks3Client("2HITWMQXL2VBB3XMAEHQ","ilZQ9p/NHAK1dOYA/dTKKeIqT/t67rO6V2PrXUNr");
		
		//putObject(bucket,key,file,client);
		//putObjectWithAdp(bucket,key,file,client);
		putAdp(bucket,key,client);
		
	}
	/**
	 * 将一个文件上传到KS3上.
	 */
	private static void putObject(String bucket,String key,File file,Ks3Client client){
		PutObjectRequest req = new PutObjectRequest(bucket,key,file);
		//设置文件的访问权限为公开读
		//如果业务需求文件时私密的，则不写以下一行代码
		req.setCannedAcl(CannedAccessControlList.PublicRead);
		
		PutObjectResult ret = client.putObject(req);
		System.out.println(ret);
	}
	/**
	 * 上传一个文件至KS3上，并触发转码等处理
	 */
	private static void putObjectWithAdp(String bucket,String key,File file,Ks3Client client){
		PutObjectRequest req = new PutObjectRequest(bucket,key,file);
		req.setCannedAcl(CannedAccessControlList.PublicRead);

		//**********************************
		//该部分为对该上传请求设置转码相关的配置
		List<Adp> adps = new ArrayList<Adp>();
		
		Adp adp = new Adp();
		//要求KS3对新上传的文件做视频截图操作，具体命令详解：http://ks3.ksyun.com/doc/video/avscrnshot.html
		adp.setCommand("tag=avscrnshot&ss=10");
		//截好的图的存储位置，访问权限将跟随视频原文件的访问权限
		adp.setKey(key+"_截图.jpeg");
		adps.add(adp);
		
		Adp adp1 = new Adp();
		//要求KS3对新上传的文件做视频转码操作，具体命令详解：http://ks3.ksyun.com/doc/video/avop.html
		adp1.setCommand("tag=avop&f=mp4&res=320x240&vbr=1000k&abr=64k");
		//转码后的文件存储位置，访问权限将跟随视频原文件的访问权限
		adp1.setKey(key+"_转码.mp4");
		adps.add(adp1);

		req.setAdps(adps);
		//由于转码是异步的，当转码操作完成后，KS3将会向向以下地址POST一段json过去，告诉客户处理结果,客户需要反馈{"result":true}
		req.setNotifyURL("http://10.4.2.38:19090/");
		//***********************************
		PutObjectResult ret = client.putObject(req);
		System.out.println(ret);
	}
	/**
	 * 对于KS3上已经存在的视频文件，可以通过该方法触发KS3对其进行视频处理。参数说明参考putObjectWithAdp
	 */
	private static void putAdp(String bucket,String key,Ks3Client client){
		PutAdpRequest req = new PutAdpRequest(bucket,key);
		List<Adp> adps = new ArrayList<Adp>();
		
		Adp adp = new Adp();
		adp.setCommand("tag=avscrnshot&ss=10");
		adp.setKey(key+"_截图.jpeg");
		adps.add(adp);
		
		Adp adp1 = new Adp();
		adp1.setCommand("tag=avop&f=mp4&res=320x240&vbr=1000k&abr=64k");
		adp1.setKey(key+"_转码.mp4");
		adps.add(adp1);

		req.setAdps(adps);
		req.setNotifyURL("http://10.4.2.38:19090/");
		PutAdpResult ret = client.putAdpTask(req);
		System.out.println(ret);
	}
}
